<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房源ID" prop="propertyId">
        <el-input
          v-model="queryParams.propertyId"
          placeholder="请输入房源ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:pending:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pendingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="待办ID" align="center" prop="pendingId" />
      <el-table-column label="房源名称" align="center" prop="propertyTitle" />
      <el-table-column label="出租者" align="center" prop="landlord" />
      <el-table-column label="租用者" align="center" prop="tenant" />
      <el-table-column label="出租者意见" align="center" prop="landlordAgreed">
        <template slot-scope="scope">
          <el-tag :type="scope.row.landlordAgreed === '1' ? 'success' : scope.row.landlordAgreed === '2' ? 'danger' : 'info'">
            {{ scope.row.landlordAgreed === '1' ? '同意' : scope.row.landlordAgreed === '2' ? '拒绝' : '待处理' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="租用者意见" align="center" prop="tenantAgreed">
        <template slot-scope="scope">
          <el-tag :type="scope.row.tenantAgreed === '1' ? 'success' : scope.row.tenantAgreed === '2' ? 'danger' : 'info'">
            {{ scope.row.tenantAgreed === '1' ? '同意' : scope.row.tenantAgreed === '2' ? '拒绝' : '待处理' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLandlordAgreement(scope.row)"
            :disabled="scope.row.landlordAgreed !== null && scope.row.landlordAgreed !== ''"
          >出租者意见</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleTenantAgreement(scope.row)"
            :disabled="scope.row.tenantAgreed !== null && scope.row.tenantAgreed !== ''"
          >租用者意见</el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改待办管理对话框 -->
    <!-- 出租者意见对话框 -->
    <el-dialog title="出租者意见" :visible.sync="landlordOpen" width="400px" append-to-body>
      <el-form ref="landlordForm" :model="landlordForm" label-width="80px">
        <el-form-item label="意见" prop="landlordAgreed">
          <el-radio-group v-model="landlordForm.landlordAgreed">
            <el-radio label="1">同意</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitLandlordAgreement">确 定</el-button>
        <el-button @click="cancelLandlordAgreement">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 租用者意见对话框 -->
    <el-dialog title="租用者意见" :visible.sync="tenantOpen" width="400px" append-to-body>
      <el-form ref="tenantForm" :model="tenantForm" label-width="80px">
        <el-form-item label="意见" prop="tenantAgreed">
          <el-radio-group v-model="tenantForm.tenantAgreed">
            <el-radio label="1">同意</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTenantAgreement">确 定</el-button>
        <el-button @click="cancelTenantAgreement">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listPending, getPending, delPending, addPending, updatePending,tenantAgreed,landlordAgreed } from "@/api/system/pending"

export default {
  name: "Pending",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待办管理表格数据
      pendingList: [],
      // 出租者对话框显示
      landlordOpen: false,
      // 租用者对话框显示
      tenantOpen: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        propertyId: null,
        status: null,
        landlordAgreed: null,
        tenantAgreed: null,
      },
      // 出租者表单参数
      landlordForm: {
        pendingId: null,
        landlordAgreed: null
      },
      // 租用者表单参数
      tenantForm: {
        pendingId: null,
        tenantAgreed: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        propertyId: [
          { required: true, message: "房源ID不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "1待房东同意 2待租客同意 3已完成 4已取消不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询待办管理列表 */
    getList() {
      this.loading = true
      listPending(this.queryParams).then(response => {
        this.pendingList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        pendingId: null,
        propertyId: null,
        status: null,
        landlordAgreed: null,
        tenantAgreed: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pendingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 出租者意见操作 */
    handleLandlordAgreement(row) {
      this.resetLandlordForm()
      this.landlordForm.pendingId = row.pendingId
      this.landlordForm.landlordAgreed = row.landlordAgreed || null
      this.landlordOpen = true
    },
    /** 租用者意见操作 */
    handleTenantAgreement(row) {
      this.resetTenantForm()
      this.tenantForm.pendingId = row.pendingId
      this.tenantForm.tenantAgreed = row.tenantAgreed || null
      this.tenantOpen = true
    },
    /** 提交出租者意见 */
    submitLandlordAgreement() {
      this.$refs["landlordForm"].validate(valid => {
        if (valid) {
          landlordAgreed(this.landlordForm).then(response => {
            this.$modal.msgSuccess("操作成功")
            this.landlordOpen = false
            this.getList()
          }).catch(error => {
            this.$modal.msgError(error.message || "操作失败")
          })
        }
      })
    },
    /** 提交租用者意见 */
    submitTenantAgreement() {
      this.$refs["tenantForm"].validate(valid => {
        if (valid) {
          tenantAgreed(this.tenantForm).then(response => {
            this.$modal.msgSuccess("操作成功")
            this.tenantOpen = false
            this.getList()
          }).catch(error => {
            this.$modal.msgError(error.message || "操作失败")
          })
        }
      })
    },
    // 取消出租者意见
    cancelLandlordAgreement() {
      this.landlordOpen = false
      this.resetLandlordForm()
    },
    // 取消租用者意见
    cancelTenantAgreement() {
      this.tenantOpen = false
      this.resetTenantForm()
    },
    // 重置出租者表单
    resetLandlordForm() {
      this.landlordForm = {
        pendingId: null,
        landlordAgreed: null
      }
      this.resetForm("landlordForm")
    },
    // 重置租用者表单
    resetTenantForm() {
      this.tenantForm = {
        pendingId: null,
        tenantAgreed: null
      }
      this.resetForm("tenantForm")
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加待办管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const pendingId = row.pendingId || this.ids
      getPending(pendingId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改待办管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pendingId != null) {
            updatePending(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addPending(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pendingIds = row.pendingId || this.ids
      this.$modal.confirm('是否确认删除待办管理编号为"' + pendingIds + '"的数据项？').then(function() {
        return delPending(pendingIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pending/export', {
        ...this.queryParams
      }, `pending_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
